//
//  MineClerkController.m
//  ScalperMerchant
//
//  Created by 王云 on 2017/10/6.
//  Copyright © 2017年 WY. All rights reserved.
//

#import "MineClerkController.h"
#import "ClerkView.h"
#import "ClerkCell.h"

@interface MineClerkController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,weak)UITableView *tableView;
@property (nonatomic,weak)ClerkView *clerkV;

@property (nonatomic,strong)NSMutableArray *memList;

@end

@implementation MineClerkController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"我的店员";
    _memList = [NSMutableArray array];
    [self setUI];
    [self loadData];
    [self memberList];
}
- (void)loadData{
    
    __weak __typeof(&*self)weakSelf = self;
    [SCApiTool ScGenerateSuccess:^(NSDictionary *dic) {
        
        [SCTool showMessage:@"请求成功"];
        
        [weakSelf.clerkV.codeImg sd_setImageWithURL:[NSURL URLWithString:[dic hw_safeObjectForKey:@"fileUrl"]] placeholderImage:[UIImage imageNamed:@"Bitmap"] options:SDWebImageRefreshCached];
        weakSelf.clerkV.WebsiteLab.text = [NSString stringWithFormat:@"  %@",[dic hw_safeObjectForKey:@"authCode"]];
        
        
    } failure:^(NSString *errormsg) {
        
        [SCTool showError:errormsg];
    }];
    
}
- (void)memberList{

    __weak __typeof(&*self)weakSelf = self;
    [SCApiTool ScMemberlistSuccess:^(NSArray *data) {
        weakSelf.memList = data.mutableCopy;
        [weakSelf.tableView reloadData];
    } failure:^(NSString *errormsg) {
        
    }];
}

- (void)setUI{
    
    __weak __typeof(&*self)weakSelf = self;
    //王云
    ClerkView *clerkV = [[[NSBundle mainBundle] loadNibNamed:@"ClerkView" owner:self options:nil] firstObject];
    clerkV.click = ^{
        [weakSelf loadData];
    };
    _clerkV = clerkV;
    UIView *baseHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,kWidth , 350)];
    clerkV.frame = CGRectMake(0, 0, kWidth, 350);
    [baseHeaderView addSubview:clerkV];
    self.tableView.tableHeaderView = baseHeaderView;

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.view);
    }];
}
#pragma mark - tableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.memList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ClerkCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ClerkCell" forIndexPath:indexPath];
    cell.dic = self.memList[indexPath.row];
    cell.click = ^(UITableViewCell *cell) {
      NSIndexPath *indexP = [tableView indexPathForCell:cell];
        [self delMem:indexP];
    };
    return cell;
}
- (void)delMem:(NSIndexPath *)index{
    
    NSDictionary *dic = self.memList[index.row];
    
    __weak __typeof(&*self)weakSelf = self;

    [SCTool showAlerts:@"删除员工?" Ok:^(id alerts) {
        
        [SCApiTool ScMemberdestoryMember_id:[NSString stringWithFormat:@"%@",[dic hw_safeObjectForKey:@"member_id"]] success:^(NSString *msg) {
            [weakSelf.memList removeObjectAtIndex:index.row];
            [weakSelf.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:index] withRowAnimation:UITableViewRowAnimationFade];
            
        } failure:^(NSString *errormsg) {
            [SCTool showError:errormsg];
        }];

        
    } Cancel:^(id alerts) {
        
    }];
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *bgV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 35)];
    bgV.backgroundColor = DarkColor;
    UILabel *titL = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kWidth, 34)];
    titL.backgroundColor = [UIColor whiteColor];
    titL.text = @"  已授权员工";
    titL.font = [UIFont boldSystemFontOfSize:15];
    titL.textColor = KOrange;
    [bgV addSubview:titL];
    return bgV;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 35;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (UITableView *)tableView{
    
    if (_tableView == nil) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.rowHeight = 70;
        tableView.separatorColor = WYColor(238, 238, 238);
        tableView.tableFooterView = [[UIView alloc] init];
        [tableView registerNib:[UINib nibWithNibName:@"ClerkCell" bundle:nil] forCellReuseIdentifier:@"ClerkCell"];
        [self.view addSubview:tableView];
        _tableView = tableView;
    }
    return _tableView;
}





@end
